//
//  ScheduledFetcher.m
//  <30>RanchForecast
//
//  Created by chen on 16/9/21.
//  Copyright © 2016年 tyrad. All rights reserved.
//

#import "ScheduledFetcher.h"
#import "ScheduledClass.h" 

@implementation ScheduledFetcher

- (instancetype)init
{
    self = [super init];
    if (self) {
        _classes = [NSMutableArray array];
        _dateFormatter = [[NSDateFormatter alloc]init];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzzz"];
    }
    return self;
}

- (NSArray *)fetchClassesWithError:(NSError **)error{
    
    BOOL success ;
    
    NSURL *xmlURL = [NSURL URLWithString:@"http://bookapi.bignerdranch.com/courses.xml"];
    
    NSURLRequest *req = [[NSURLRequest alloc]initWithURL:xmlURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
    
    NSURLResponse *resp = nil ;
    NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:error];
    if (data == nil) {
        return  nil ;
    }
    
    //清空classes
    [_classes removeAllObjects];
    
    NSXMLParser *parsel  = [[NSXMLParser alloc]initWithData:data];
    [parsel setDelegate:self];
    
    success = [parsel parse];  //调用开始解析数据，如果返回YES就是解析成功了
    
    if (success == NO) {
        *error = [parsel parserError];
        return nil;
    }
    
    NSArray *output = [_classes copy];
    return output;
}



#pragma mark -- NSXMLParserDelegate



- (void)    parser:(NSXMLParser *)parser
   didStartElement:(NSString *)elementName
      namespaceURI:(nullable NSString *)namespaceURI
     qualifiedName:(nullable NSString *)qName
        attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"class"]) {
        
        _currentFeilds = [NSMutableDictionary dictionary];
        
    }else if([elementName isEqualToString:@"offering"]){
        NSLog(@"%@" ,attributeDict);
        [_currentFeilds setObject:[attributeDict objectForKey:@"href"] forKey:@"href"];
    }
    
//    else if ([elementName isEqualToString:@"instructor"]){
//        
//        NSLog(@"%@ %@",_currentFeilds , attributeDict );
//        _currentFeilds[@"name"] =  attributeDict[@"instructor"];
//        
//    }else if ([elementName isEqualToString:@"location"]){
//        
//        _currentFeilds[@"location"] =  attributeDict[@"location"];
//
//    }else if ([elementName isEqualToString:@"begin"]){
//        
//        _currentFeilds[@"begin"] =  attributeDict[@"begin"];
//    }
}


- (void)    parser:(NSXMLParser *)parser
     didEndElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI
     qualifiedName:(NSString *)qName
{
    
    NSLog(@"%@",_currentFeilds);

    if ([elementName isEqualToString:@"class"])
    {
        NSLog(@"%@",_currentFeilds);
//        ScheduledClass * class = [[ScheduledClass alloc]init];
//        class.name = curr;
//        class.location = ;
//        class.href = ;
//        
//        class.name = ;
    }
    
    
    
    
}









@end
















